﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Data;
using SdmxMl.Common;

namespace SdmxMl.Structure
{

    /// <summary>
    /// Group ContactType choice attributes for editing purpose
    /// </summary>
    public class ContactInfo : IDatatableExchange
    {
        #region Internal Members

        public List<string> TelephoneList;
        public List<string> FaxList;
        public List<string> X400List;
        public List<string> UriList;
        public List<string> EmailList;

        #endregion //Internal Members

        #region Construction

        public ContactInfo()
        {
            Init();
        }

        private void Init()
        {
            TelephoneList = new List<string>();
            FaxList = new List<string>();
            X400List = new List<string>();
            UriList = new List<string>();
            EmailList = new List<string>();
        }

        public ContactInfo(XmlNode nodeArtefact, XmlNamespaceManager nsmgr)
        {
            Init();
            UpdateInfoList(TelephoneList, "./structure:Telephone", nodeArtefact, nsmgr);
            UpdateInfoList(FaxList, "./structure:Fax", nodeArtefact, nsmgr);
            UpdateInfoList(X400List, "./structure:X400", nodeArtefact, nsmgr);
            UpdateInfoList(UriList, "./structure:URI", nodeArtefact, nsmgr);
            UpdateInfoList(EmailList, "./structure:Email", nodeArtefact, nsmgr);
        }

        private void UpdateInfoList(List<string> list, string query, XmlNode nodeArtefact, XmlNamespaceManager nsmgr)
        {
            XmlNodeList nodeList = nodeArtefact.SelectNodes(query, nsmgr);
            foreach (XmlNode na in nodeList)
                list.Add(na.InnerText);
        }

        #endregion //Construction

        #region Clone

        public ContactInfo Clone()
        {
            ContactInfo c = new ContactInfo();

            // Clone lists content
            c.EmailList = new List<string>(this.EmailList);
            c.TelephoneList = new List<string>(this.TelephoneList);
            c.X400List = new List<string>(this.X400List);
            c.UriList = new List<string>(this.UriList);
            c.EmailList = new List<string>(this.EmailList);

            return c;
        }
        #endregion

        #region XML serialize

        /// <summary>Nax rows from any of choice attributes</summary>
        /// <returns>rows maxi</returns>
        public int MaxRows()
        {
            int nb = TelephoneList.Count;
            nb = Math.Max(nb, FaxList.Count);
            nb = Math.Max(nb, X400List.Count);
            nb = Math.Max(nb, UriList.Count);
            nb = Math.Max(nb, EmailList.Count);
            return nb;
        }

        public void WriteXml(XmlTextWriter xw, string prefix, string tag)
        {
            if (MaxRows() > 0)
            {
                foreach (string s in TelephoneList)
                    xw.WriteElementString(prefix, "Telephone", null, s);
                foreach (string s in FaxList)
                    xw.WriteElementString(prefix, "Fax", null, s);
                foreach (string s in X400List)
                    xw.WriteElementString(prefix, "X400", null, s);
                foreach (string s in UriList)
                    xw.WriteElementString(prefix, "URI", null, s);
                foreach (string s in EmailList)
                    xw.WriteElementString(prefix, "Email", null, s);
            }
        }
        #endregion //XML serialize

        #region IDatatableExchange

        /// <summary>Presents info as datatable</summary>
        /// <returns>a datatable representation</returns>
        public DataTable AsDataTable()
        {
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add(new DataColumn("Tel", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Email", typeof(string)));
            dataTable.Columns.Add(new DataColumn("Fax", typeof(string)));
            dataTable.Columns.Add(new DataColumn("URI", typeof(string)));
            dataTable.Columns.Add(new DataColumn("X400", typeof(string)));

            for (int r = 0; r < MaxRows(); r++)
                dataTable.Rows.Add(new object[] 
                { 
                    TelephoneList.Count > r ? TelephoneList[r] : string.Empty,
                    EmailList.Count > r ? EmailList[r] : string.Empty,
                    FaxList.Count > r ? FaxList[r] : string.Empty,
                    UriList.Count > r ? UriList[r] : string.Empty,
                    X400List.Count > r ? X400List[r] : string.Empty,
                });

            return dataTable;
        }

        public void FromDataTable(DataTable tb)
        {
            TelephoneList.Clear();
            EmailList.Clear();
            FaxList.Clear();
            UriList.Clear();
            X400List.Clear();
            foreach (DataRow row in tb.Rows)
            {
                if (row["Tel"].ToString().Length > 0) TelephoneList.Add(row["Tel"].ToString());
                if (row["Email"].ToString().Length > 0) EmailList.Add(row["Email"].ToString());
                if (row["Fax"].ToString().Length > 0) FaxList.Add(row["Fax"].ToString());
                if (row["URI"].ToString().Length > 0) UriList.Add(row["URI"].ToString());
                if (row["X400"].ToString().Length > 0) X400List.Add(row["X400"].ToString());
            }
        }
        #endregion //IDatatableExchange

    }
}
